package com.xywy.find.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.xywy.R;
import com.xywy.common.util.ImageLoaderUtil;
import com.xywy.common.util.MLog;
import com.xywy.find.bean.JkzhArticle;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 *  发现 - 健康知乎 - item
 */
public class JkzhItemAdapter extends BaseAdapter {
    private static final String TAG = "JkzhItemAdapter";
    private Context mContext;
    private List<JkzhArticle> list;

    private int target;
    private String  title;

    public JkzhItemAdapter(Context context,String title) {
        this.mContext = context;
        this.title = title;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void setData(List<JkzhArticle>  stringList){
        this.list = stringList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hold hold;
        if(convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.find_jkzh_plaze_item, null);

            hold = new Hold();
            hold.ll = (LinearLayout)convertView.findViewById(R.id.ll_jkzh_item);
            hold.title = (TextView) convertView.findViewById(R.id.tv_jkzh_item_title);
            hold.name = (TextView) convertView.findViewById(R.id.tv_jkzh_item_name);
            hold.type = (TextView) convertView.findViewById(R.id.tv_jkzh_item_type);
            hold.reply = (TextView) convertView.findViewById(R.id.tv_jkzh_item_reply);
            hold.docImg = (ImageView) convertView.findViewById(R.id.iv_jkzh_item_img);
            hold.linear = (LinearLayout) convertView.findViewById(R.id.linear);
            
            convertView.setTag(hold);
        }else{
            hold = (Hold) convertView.getTag();
        }
        final JkzhArticle data=list.get(position);

       /* hold.ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    v.setBackgroundColor(mContext.getResources().getColor(R.color.find_jkgc_item_bg));
                    Intent intent = new Intent(mContext, JkzhArticleActivity.class);
                    intent.putExtra("artid", data.getArtId());
                    intent.putExtra("title",title);
                    mContext.startActivity(intent);
                }
                else if(event.getAction() == MotionEvent.ACTION_UP){
                    v.setBackgroundColor(Color.WHITE);

                }
                return false;
            }
        });*/


        hold.title.setText(String.valueOf(data.getTitle()));
        hold.name.setText(String.valueOf(data.getNameD()));
        hold.type.setText(String.valueOf(data.getType()));
        hold.reply.setText(String.valueOf(data.getContent()));

//        imageLoader.displayImage(data.getDocImg(), hold.docImg, options, new SimpleImageLoadingListener() {
//            @Override
//            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                if (loadedImage != null) {
//                    ((ImageView) view).setScaleType(ImageView.ScaleType.CENTER_CROP);
//                    boolean firstDisplay = !displayedImages.contains(imageUri);
//                    if (firstDisplay) {
//                        FadeInBitmapDisplayer.animate((ImageView) view, 500);
//                        displayedImages.add(imageUri);
//                    }
//                }
//            }
//            @Override
//            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                Throwable cause = failReason.getCause();
////                MLog.e(TAG, cause.toString());
//            }
//        });
        hold.linear.setBackgroundResource(R.drawable.common_list_selector);
        
          ImageLoaderUtil.displayImage(data.getDocImg(), hold.docImg);
        return convertView;
    }
    public class Hold{
          LinearLayout ll;
          TextView  title;
          TextView  name;
          TextView  type;
          TextView  reply;
          ImageView  docImg;
          LinearLayout linear;
    }


    //====ImageLoader start====
    public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
    //ImageLoaderOptions
    private DisplayImageOptions options;

    //ImageLoader
    protected ImageLoader imageLoader = ImageLoader.getInstance();

    private void initImageLoader() {

        options = new DisplayImageOptions.Builder()
//                .showImageForEmptyUri(R.drawable.ic_error) // resource or drawable
                .showImageOnFail(R.drawable.imageloader_error) // resource or drawable
                .showStubImage(R.drawable.imageloader_default)
                .resetViewBeforeLoading(false)  // default
                .cacheInMemory(true)
                .cacheOnDisc(true)
                        //精准
                .imageScaleType(ImageScaleType.EXACTLY)
                        //ARGB_8888  每个像素要4字节~带透明度的颜色, 默认色样
                        //RGB_565    每个像素要2字节~不带透明度的颜色
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(10))
                .build();
    }
    //====ImageLoader end====
}
